﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace test
{
    using System.IO;

    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        public Stream ShowOpenFile()
        {
            OpenFileDialog dlg = new OpenFileDialog { Filter = "NES Roms (*.nes)|*.nes", Multiselect = false };
            if (dlg.ShowDialog().GetValueOrDefault(false))
            {
                return dlg.File.OpenRead();
            }
            return null;
        }


        private void LayoutRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!e.Handled && !savea)
            {
                if (!game.Started)
                {
                    Stream rom = ShowOpenFile();
                    game.LoadRom(rom);
                }
            }
        }

        private bool savea = false;

        private Stream ShowSaveAudio()
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.DefaultExt = ".wav";
            if (sfd.ShowDialog().GetValueOrDefault())
            {
                return sfd.OpenFile();
            }
            return null;
        }

        private void Ellipse_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            savea = true;
            Stream audios = ShowSaveAudio();
            game.SetAudioSave(audios);
            savea = false;
            record.Visibility = System.Windows.Visibility.Collapsed;
            e.Handled = true;
        }
    }
}
